﻿Option Explicit On
Option Strict On

Partial Class _default
    Inherits System.Web.UI.Page
    Dim u As MembershipUser

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub

    'Protected Sub btnShowEntityes_Click(sender As Object, e As System.EventArgs) Handles btnShowEntityes.Click

    '    pnlEntityes_ModalPopupExtender.Show()

    'End Sub

    'Protected Sub btnShowDetails_Click(sender As Object, e As System.EventArgs) Handles btnShowDetails.Click
    '    pnlDetails_ModalPopupExtender.Show()
    'End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        'Dim virtualurl As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
        '    HttpContext.Current.Request.Url.Segments(1).ToString() + "Account/Activate.aspx?id=").Replace("Account/Account/", "Account/")
        'Dim virtualurl1 As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
        '    HttpContext.Current.Request.Url.Segments(1).ToString() + "private/secure_admin/default.aspx?id=").Replace("private/private/", "private/")

        Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()
        Dim strMailTo As String = ConfigurationManager.AppSettings("accountapprovalemail").ToString()
        Dim strApprovalEmail As String = strMailTo
        Dim sendemail As New SendEmailViaGoogle()

        Dim strMessage As String = "FYI: A Test message " + u.ProviderUserKey.ToString() + "</br>"
        'strMessage += "FYI: A new user [" + u.UserName + "] has registered. " + virtualurl1 + u.ProviderUserKey.ToString() + "</br>"
        Dim strSubject As String = "A Test message"

        Dim strRecipient As String = strApprovalEmail


        ' Replace placeholder text in message body with information provided by the user.

        '------------------------------------------------------------------------------------------------------
        ' original code 
        '------------------------------------------------------------------------------------------------------
        'Dim virtualurl As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
        '    HttpContext.Current.Request.Url.Segments(1).ToString() + "Account/Activate.aspx?id=").Replace("Account/Account/", "Account/")

        Dim virtualurl As String = "http://bbc.co.uk/news" 'replace this with the original email
        Dim strCategory As String = "Register"

        'Original code
        'e.Message.Body = e.Message.Body.Replace("<%link%>", virtualurl + u.ProviderUserKey.ToString())
        '------------------------------------------------------------------------------------------------------

        'This is the new tracking destination
        'So send the user to this page to pick up the link to forward to the original destination
        Dim strTrackingURL As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
            HttpContext.Current.Request.Url.Segments(1).ToString() + "admin/general/efwd.aspx?ref=").Replace("Admin/Admin/", "Admin/")

        'This is a copy of what would have been originally sent - we need to save this in database for when the user clicks the substitute
        Dim strForwardLink As String = strTrackingURL + u.ProviderUserKey.ToString() + "&cat="

        strMessage = strForwardLink + strCategory

        '-------------------------------------------------------------
        'TODO: 16-Mar-2013 - Tony S.
        '-------------------------------------------------------------
        Try

            'Now substitute the outgoing url for a new guid and write both the guid and url to a table - dbo.eForward
            'Dim newguid As String = New Guid().ToString()
            Dim efwd_Insert As New LogginsLogDSTableAdapters.SPTableAdapter
            efwd_Insert.usp_eForward_Insert(u.ProviderUserKey.ToString(), strCategory, virtualurl)

        Catch ex As Exception
            Me.lblMessage.Text = "Item not saved to email tracking table"
            Throw ex
        End Try
        '-------------------------------------------------------------

        Try

            'Send the email to the account approver
            sendemail.SendEmail(strMailFrom, strMailTo, strSubject, strMessage)

            Me.lblMessage.Text = "Message sent successfully"
        Catch ex As Exception

            'Do something with this error, 
            'Maybe send it back to be handled in global.asax
            Me.lblMessage.Text = "Message NOT sent successfully"
            Throw ex

        End Try

    End Sub
End Class
